package com.zhouzj.task.zookeeper;

import com.zhouzj.task.zookeeper.common.rpc.ConfigKeeper;
import com.zhouzj.task.zookeeper.consumer.RpcConsumer;
import com.zhouzj.task.zookeeper.export.api.user.IUserService;
import com.zhouzj.task.zookeeper.export.dto.User;
import com.zhouzj.task.zookeeper.registry.handler.RpcRegistryHandler;
import com.zhouzj.task.zookeeper.registry.handler.impl.ZookeeperRegistryHandler;

import java.util.HashMap;
import java.util.Map;

public class ConsumerApplication {

    public static void main(String[] args) throws InterruptedException {

        Map<String, Object> instanceCacheMap = new HashMap<>();
        instanceCacheMap.put(IUserService.class.getName(), IUserService.class);

        ConfigKeeper.getInstance().setConsumerSide(true);
        // 启动一个定时的线程池，每隔xx秒开始自动上报统计数据到注册中心
        ConfigKeeper.getInstance().setInterval(5);

        RpcRegistryHandler rpcRegistryHandler = new ZookeeperRegistryHandler("127.0.0.1:2181");
        RpcConsumer consumer = new RpcConsumer(rpcRegistryHandler, instanceCacheMap);

        IUserService userService = (IUserService) consumer.createProxy(IUserService.class);

        while (true) {
            Thread.sleep(2000);
//            userService.sayHello("are you ok?");
            User user = userService.user(2);
            System.out.println(user.getName());
        }

    }

}
